package com.mf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.request.RoleSaveRequest;
import com.mf.service.IRoleService;
import io.github.heollhai.common.config.exceptions.GraceException;
import io.github.heollhai.common.entity.bo.RoleBo;
import io.github.heollhai.common.entity.request.publicRequest.PublicBindingIdRequest;
import io.github.heollhai.common.entity.request.publicRequest.PublicUpdateStatusRequest;
import io.github.heollhai.common.enums.EnableOrDisableEnum;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.VerifyUtils;
import io.github.heollhai.common.utils.enums.EnumUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色表
 *
 * @author mf
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/auth/role/v1")
@Tag(name = "角色管理")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @Operation(summary = "新增|保存角色")
    @PostMapping("save")
    public Result<Integer> save(@Valid @RequestBody RoleSaveRequest roleSaveRequest) {
        RoleBo roleBo = new RoleBo();
        BeanUtils.copyProperties(roleSaveRequest, roleBo);
        if (VerifyUtils.isEmpty(roleSaveRequest.getId())) {
            roleService.save(roleBo);
        }else {
            roleService.updateById(roleBo);
        }
        return Result.success(null);
    }

    @Operation(summary = "修改角色状态")
    @GetMapping("updateStatus")
    public Result<Boolean> updateStatus(@Valid @RequestBody PublicUpdateStatusRequest<Integer> updateStatusRequest) {
        if(EnumUtils.existsInEnum(EnableOrDisableEnum.class, updateStatusRequest.getStatus())){
            GraceException.display("状态异常");
        }
        RoleBo roleBo = new RoleBo();
        BeanUtils.copyProperties(updateStatusRequest, roleBo);
        return Result.success(roleService.updateById(roleBo));
    }
    
    @Operation(summary = "删除角色")
    @GetMapping("deleteById/{id}")
    public Result<Integer> deleteById(@PathVariable("id") Integer id) {
        return Result.success(roleService.removeRoleById(id));
    }
    
    @Operation(summary = "查询所有角色")
    @GetMapping("findList")
    public Result<List<RoleBo>> findList() {
        LambdaQueryWrapper<RoleBo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleBo::getStatus, EnableOrDisableEnum.ENABLE.value);
        return Result.success(roleService.list(queryWrapper));
    }

    @Operation(summary = "根据用户绑定角色")
    @PostMapping("userBindingRole")
    public Result<Boolean> userBindingRole(@RequestBody PublicBindingIdRequest bindingIdRequest) {
        return Result.success(roleService.userBindingRole(bindingIdRequest));
    }

    @Operation(summary = "根据用户id查询绑定的角色id")
    @GetMapping("findListByUserId")
    public Result<List<Integer>> findListByRoleId(@RequestParam("id") Integer userId) {
        return Result.success(roleService.findListByRoleId(userId));
    }
}
